package cn.meye11.crm.controller;

import cn.meye11.crm.common.RestResponse;
import cn.meye11.crm.domain.model.User;
import cn.meye11.crm.domain.query.UserQuery;
import cn.meye11.crm.service.UserService;
import jakarta.servlet.http.HttpSession;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * (User)表服务控制类
 *
 * @author makejava
 * @since 2023-11-16 14:48:00
 */
@Slf4j
@Controller
@RequestMapping("user")
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class UserController {

    private final UserService userService;

    // 代码写到此区域

    /**
     * 登录方法
     *
     * @param name     用户名
     * @param password 密码
     * @param session  HttpSession对象，用于保存用户登录状态
     * @param model    Model对象，用于向页面传值
     * @return 登录成功返回"index"，登录失败返回"login"
     */
    @PostMapping("login")
    public String login(String name, String password, HttpSession session, Model model) {
        User user = userService.checkLogin(name, password);
        if (user == null) {
            model.addAttribute("flag", "用户名或者密码错误");
            return "login";
        } else {
            session.setAttribute("user", user);
            return "index";
        }
    }

    /**
     * 登出用户
     *
     * @param session HttpSession对象
     * @return 登出后重定向到登录页面
     */
    @GetMapping("logout")
    public String logout(HttpSession session) {
        session.invalidate();
        return "login";
    }

    @GetMapping("manage")
    public String manage(HttpSession session) {
        session.invalidate();
        return "user/user";
    }

    /************************************* 请在上方编写新代码 **************************************/
    // 此区域为自动生成的通用代码 尽量不直接修改
    @PutMapping("save")
    @ResponseBody
    public RestResponse<Long> save(User model) {

        Long id = userService.save(model);
        return RestResponse.success("成功", id);
    }

    @DeleteMapping("remove/{id}")
    @ResponseBody
    public RestResponse<String> remove(User model) {

        userService.remove(model);
        return RestResponse.success("成功");
    }

    @PostMapping("update")
    @ResponseBody
    public RestResponse<String> update(User model) {

        if (model.getId() == null) {
            throw new RuntimeException("ID不能为空");
        }
        userService.update(model);
        return RestResponse.success("成功");
    }

    @GetMapping("get/{id}")
    @ResponseBody
    public RestResponse<User> get(Integer id) {
        User model = userService.get(id);
        return model == null ? RestResponse.failed("不存在") : RestResponse.success("成功", model);
    }

    public RestResponse<Long> count(UserQuery query) {
        Long count = userService.count(query);
        return RestResponse.success("成功", count);
    }

    @GetMapping("list")
    @ResponseBody
    public RestResponse<List<User>> list(UserQuery query) {
        return userService.list(query);
    }

    @GetMapping("page")
    @ResponseBody
    public RestResponse<List<User>> page(UserQuery query) {
        return userService.page(query);
    }
}
